import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import beans.IdValue;
import beans.Person;
import beans.Project;
import beans.ResourceSchedule;
import db.DataUtils;
import db.PersonDAO;
import db.ProjectDAO;
import db.SQL;
import db.TaskDAO;


public class Dao {

	/**
	 * Gets person.
	 * 
	 * @param email identifying person
	 * @return person or null if person doesn't exist.
	 */
	public static Person getPerson(String email) {
		if(email == null || email.trim().length() == 0) {
			return null;
		}
		return PersonDAO.getPerson(email);
	}
	
	/**
	 * Gets skills a person has acquired or needs.
	 * 
	 * @param person
	 * @return List of skills, or the empty list if
	 * person has not acquired nor needs any skills.
	 */
	public static List getSkills(Person person) {
		return PersonDAO.getSkills(person);
	}
		
	/**
	 * Gets projects and tasks a person is assigned.
	 * 
	 * @param person
	 * @return List of projects, or the empty list if
	 * person is not assigned any.  Projects need not
	 * be returned in any particular order, but tasks
	 * inside projects should be returned ordered by
	 * increasing sequence number.
	 */
	public static List getAssignedProjects(Person person) {
		List projects = ProjectDAO.getAssignedProjects(person);
		
		for (int i = 0; i < projects.size(); i++) {
			Project project = (Project) projects.get(i);
			ProjectDAO.getTasks(project, person);
		}
		
		return projects;
	}
	
	/**
	 * Gets project titles managed by person.
	 *  
	 * @param person
	 * @return List of project titles, or the empty
	 * list if person does not manage any projects.
	 * The returned list consists of objects of type IdValue
	 * in which the id is the project id and the
	 * value is the project title.
	 */
	public static List getManagedProjectTitles(Person person) {	
		try {
			
			Connection c = DataUtils.getDBConnection();
			
			PreparedStatement st = c.prepareStatement(SQL.map.get("SMANAGERPROJECTS.sql").toString());
			st.setInt(1, person.id);
			
			ResultSet rs = st.executeQuery();
		    
		    List projectList = new ArrayList();
		    
		    while (rs.next()) {
			    int projId = rs.getInt("PROJ_ID"); 
			    String title = rs.getString("TITLE");
			    projectList.add(new IdValue(projId, title));
		    }

		    rs.close();
		    st.close();
		    
		    return projectList;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * Gets task metrics for all tasks in the project
	 * identified by projectId.
	 * 
	 * @param projectId
	 * @return List of TaskMetrics or the empty list
	 * if the project has no tasks.  The list should be
	 * ordered by ascending task sequence number.
	 */
	public static List getTaskMetricsForProject(int projectId) {
		return TaskDAO.getTaskMetricsForProject(projectId);
	}
	
	/**
	 * Gets people and metadata about people assigned
	 * to the project identified by projectId.
	 *  
	 * @param projectId
	 * @return List of PersonAssignment or the empty
	 * list if no people are assigned to the project.
	 */
	public static List getPeopleAssignedToProject(int projectId) {
		return PersonDAO.getPeopleAssignedToProject(projectId);
	}
	
	
}
